package com.item 
{
	import com.data.GameData;
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author wen
	 */
	public class BellsView 
	{	
		private const NUMS:int = 20;
		
		private const LEVELS:int = 40;
		
		private var _con:MovieClip = null;
		
		private var _currentBell:Bells = null;
		
		private var _hasAddList:Array = null;
		
		private var _bellsList:Array = null;
		
		private var _bellsNum:int = 0;
		
		public function BellsView() 
		{
			
		}
		
		public function init(con:MovieClip):void
		{
			_con = con;
			_hasAddList = new Array();
			_bellsList = new Array();
			
			initBells();
		}
		
		private function initBells():void 
		{
			for (var i:int = 0; i < LEVELS; i++ )
			{
				for (var j:int = 0; j < NUMS; j++ )
				{
					var o:Object = new Object();
					o.id = i * NUMS + j;
					o.level = i;
					o.x = 20 + Math.random() * Math.min(GameData.gameWidth-30, (330 + i * 100));
					o.y = 240 - 120 * (j + NUMS * i);
					
					_bellsList.push(o);
				}
			}
		}
		
		public function initItem():void
		{
			for (var i:int = 0; i < 5; i++ )
			{
				addClildBell();
			}	
		}
		
		public function addClildBell():void
		{	
			if (_bellsNum < _bellsList.length)
			{
				var tmpMc:bellMc = new bellMc();
				var tmpBell:Bells = new Bells(tmpMc);
				var o:Object = _bellsList[_bellsNum];
				
				tmpBell.setScale(o.level);
				
				_hasAddList.push(tmpBell);
				
				_con.addChild(tmpMc);
				
				tmpMc.x = o.x;
				tmpMc.y = o.y;
				tmpBell.id = o.id;
				_bellsNum++;
			}
		}
		
		
		public function deleteBell(num:int):void
		{
			(_hasAddList[num] as Bells).clearView();
			_hasAddList.splice(num, 1);
		}
		
		public function get currentBell():Object 
		{
			return _currentBell;
		}
		
		public function set currentBell(value:Object):void 
		{
			_currentBell = _hasAddList[value.num] as Bells;
			
			_currentBell.stepOn(value.score);
			
			_hasAddList.splice(value.num, 1);
			
			for (var i:int = 0; i < (6 - _hasAddList.length); i++ ) 
			{
				addClildBell();
			}
		}
		
		public function clear():void
		{
			for (var i:int = 0; i < _hasAddList.length; i++ )
			{
				(_hasAddList[i] as Bells).clearView();
			}
			
			_hasAddList = new Array();
			_bellsList = new Array();
			initBells();
			
			_currentBell = null;
			_bellsNum = 0;
		}
		
		public function get hasAddList():Array 
		{
			return _hasAddList;
		}
	}

}